//
//  MapViewController.m
//  Created by Devin Ross on 7/11/09.
//
/*
 
 tapku.com || http://github.com/tapku/tapkulibrary/tree/master
 
 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the "Software"), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:
 
 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 
 */

#import "MapViewController.h"

#import <MapKit/MapKit.h>
#import "TKMapView.h"
#import "TKMapPlace.h"

@implementation MapViewController

@synthesize locationManager;
@synthesize detailString;
@synthesize lat,lon,myLat,myLon;

- (id)initWithFrame:(CGRect)rect
{
	[super init];
	self.view.frame = rect;
	
	return self;
	
}

- (void)viewDidLoad {
	self.view.frame=CGRectMake(0, 0, 660, 500);
    [super viewDidLoad];
	mapView = [[TKMapView alloc] initWithFrame:self.view.bounds];
	mapView.delegate = self;
	[self.view addSubview:mapView];
	
	CLLocationCoordinate2D corner;
	corner.latitude = lat; // up and down
	corner.longitude = lon; // left right
	
	[self initLocation];
	[self getLocationBtnClicked:nil];
	
	[self didPlacePinAtCoordinate:corner];
	

	
	[self regionSet];
	


}

-(void)regionSet{
	CLLocationCoordinate2D corner;
	corner.latitude = lat; // up and down
	corner.longitude = lon; // left right
	
	MKCoordinateRegion region;
	region.center=corner;
	//Set Zoom level using Span
	MKCoordinateSpan span;
	span.latitudeDelta=.005;
	span.longitudeDelta=.005;
	region.span=span;
	
	[mapView.mapView setRegion:region animated:TRUE];
	
}

-(void)regionSetMy{
	CLLocationCoordinate2D corner;
	corner.latitude = myLat; // up and down
	corner.longitude = myLon; // left right
	

	
	
	MKCoordinateRegion region;
	region.center=corner;
	//Set Zoom level using Span
	MKCoordinateSpan span;
	span.latitudeDelta=.005;
	span.longitudeDelta=.005;
	region.span=span;
	
	[mapView.mapView setRegion:region animated:TRUE];
	
}


- (void) addPinMode:(id)sender{
	UIButton *button=(UIButton *)sender;
	if(mapView.pinMode){
		mapView.mapView.mapType = MKMapTypeStandard;
		
	}
		
	else{
		mapView.mapView.mapType = MKMapTypeHybrid;
	}
		
	[mapView setPinMode:!mapView.pinMode];
}

-(void)showPlacePin{
	CLLocationCoordinate2D corner;
	corner.latitude = lat; // up and down
	corner.longitude = lon; // left right
	
	[self didPlacePinAtCoordinate:corner];
}

- (void) didPlacePinAtCoordinate:(CLLocationCoordinate2D)location{
	TKMapPlace *place = [[TKMapPlace alloc] init];
	place.title = [NSString stringWithFormat:@"%@",detailString];
	NSLog(@"(%f,%f)",location.latitude,location.longitude);
	place.coordinate = location;
	
	[mapView.mapView addAnnotation:place];
	[place release];
}

- (void) didPlaceMyLocation:(CLLocationCoordinate2D)location{
	TKMapPlace *place = [[TKMapPlace alloc] init];
	place.title = @"我的位置";
	NSLog(@"(%f,%f)",location.latitude,location.longitude);
	place.coordinate = location;
	[place setColor:MKPinAnnotationColorGreen]; 
	[mapView.mapView addAnnotation:place];
	[place release];
}

- (void)dealloc {
	[mapView release];
    [super dealloc];
}


//位置管理器代理方法
#pragma mark -
#pragma mark CLLocationManagerDelegate Methods

//初始化定位相关参数
-(void) initLocation
{
	self.locationManager = [[CLLocationManager alloc] init];
	//设置代理
	locationManager.delegate=self;
	//设置需要的定位精度
	locationManager.desiredAccuracy=kCLLocationAccuracyBest;
}

-(IBAction) getLocationBtnClicked:(id) sender
{
	//启动定位
	[locationManager startUpdatingLocation];
}

//确定当前位置和位置更新了时调用这个方法
- (void)locationManager:(CLLocationManager *)manager 
	didUpdateToLocation:(CLLocation *)newLocation 
		   fromLocation:(CLLocation *)oldLocation
{
	//获得定位点的纬度信息
	//NSString *latitudeString=[[NSString alloc] initWithFormat:@"%g",newLocation.coordinate.latitude];
	myLat=newLocation.coordinate.latitude;
	//获得定位点的经度信息
	//NSString *longitudeString=[[NSString alloc] initWithFormat:@"%g",newLocation.coordinate.longitude];
	myLon=newLocation.coordinate.longitude;
	
	//停止定位
	[locationManager stopUpdatingLocation];
	
	CLLocationCoordinate2D corner;
	corner.latitude = myLat; // up and down
	corner.longitude = myLon; // left right
	
	[self didPlaceMyLocation:corner];
	//把地图中心移动到定位到的这个点
	//[mapView setCenterCoordinate:newLocation.coordinate animated:YES];
	//重新设置地图显示的区域
	
}
//位置查询遇到错误时调用这个方法
- (void)locationManager:(CLLocationManager *)manager 
	   didFailWithError:(NSError *)error
{
	NSString *errorType = (error.code == kCLErrorDenied) ? 
    @"Access Denied" : @"Unknown Error";
    UIAlertView *alert = [[UIAlertView alloc] 
                          initWithTitle:@"Error getting Location" 
                          message:errorType 
                          delegate:nil 
                          cancelButtonTitle:@"Okay" 
                          otherButtonTitles:nil];
    [alert show];
    [alert release];
}

@end
